<!-- 系统安全界面 -->、
<template>
    <div class="content">
        <el-divider content-position="left">系统安全</el-divider>
        <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm" labelPosition="left">
            <el-row>
                <el-col :span="8">
                    <el-form-item label="当前登录用户">
                        <el-input v-model="username" style="width: 55%;"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="修改用户名">
                        <el-switch v-model="ruleForm.changeIsName"></el-switch>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="初始密码修改">
                        <el-switch v-model="ruleForm.changeIsPwd"></el-switch>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>

                <el-col :span="8">
                    <el-form-item label="密码期限限制">
                        <el-switch v-model="ruleForm.pwdIsDate"></el-switch>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="密码有效期限">
                        <el-input style="width: 55%;" v-model="ruleForm.pwdDate" type="number" 
                            :disabled="!ruleForm.pwdIsDate"></el-input> 天
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="弱口令校验">
                        <el-switch v-model="ruleForm.pwdIsWeak"></el-switch>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="设置密码长度">
                        <el-input type="number" min="5" style="width: 35%;"
                            v-model="ruleForm.pwdMinLength"></el-input>--<el-input type="number" min="5"
                            style="width: 35%;" v-model="ruleForm.pwdMaxLength"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="16">
                    <el-form-item label="密码复杂度">
                        <el-checkbox-group v-model="ruleForm.pwdComplexity">
                            <el-checkbox v-for="(pwdCom, index) in pwdComList" :label="index+''" :key="pwdCom">{{ pwdCom
                                }}</el-checkbox>
                        </el-checkbox-group>
                    </el-form-item>

                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="密码错误限制">
                        <el-switch v-model="ruleForm.pwdIsError"></el-switch>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="密码错误次数">
                        <el-input type="number" min="1" style="width: 55%;" v-model="ruleForm.pwdErrorNum"
                            :disabled="!ruleForm.pwdIsError"></el-input> 次
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="限制登录时间">
                        <el-input type="number" min="1" style="width: 55%;" v-model="ruleForm.pwdErrorTime"
                            :disabled="!ruleForm.pwdIsError"></el-input>
                        分钟
                    </el-form-item>
                </el-col>

            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="开启登录保护">
                        <el-switch v-model="ruleForm.loginIsProtect"></el-switch>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="登录保护时间">
                        <el-input type="number" min="1" style="width: 55%;" :disabled="!ruleForm.loginIsProtect"
                            v-model="ruleForm.loginProtectTime"></el-input> 分钟
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>

                <el-col :span="8">
                    <el-form-item label="多人同时登录">
                        <el-switch v-model="ruleForm.isSingleLogin"></el-switch>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-button type="primary" @click="onSubmit">保存设置</el-button>
                    <el-button @click="goback">返回</el-button>
                </el-col>
            </el-row>


        </el-form>
    </div>
</template>
<script>

import {
    setSystemSafety, getSystemSafety
} from "@/api/systemSecurity/systemSecurity.js";
export default {
    data() {
        return {
            pwdComList: [
                '密码必须包含字母和数字', '密码中字母必须包含大小写', '密码中必须包含特殊符号'
            ],
            username: '',
            ruleForm: {
                id: null,
                // 用户名是否支持修改
                changeIsName: false,
                // 初始密码是否修改
                changeIsPwd: false,
                // 密码是否设置有效期
                pwdIsDate: false,
                // 密码有效期
                pwdDate: null,
                // 密码最小长度
                pwdMinLength: null,
                // 密码最大长度
                pwdMaxLength: null,
                // 密码复杂度
                pwdComplexity: [],
                // 密码错误次数是否限制
                pwdIsError: true,
                // 密码错误次数
                pwdErrorNum: null,
                // 限制登录时间
                pwdErrorTime: null,
                // 弱口令校验
                pwdIsWeak: false,
                // 是否开启登录保护
                loginIsProtect: false,
                // 登录保护时间
                loginProtectTime: null,
                // 是否允许多人同时登录一个账号
                isSingleLogin: false
            },

        };
    },
    methods: {
        onSubmit() {
            if(this.ruleForm.pwdMinLength>=this.ruleForm.pwdMaxLength){
                this.$message({
                    type: 'error',
                    message: '密码最大长度应大于密码最小长度'
                });
                return
            }
            if(this.ruleForm.pwdMinLength<5){
                this.$message({
                    type: 'error',
                    message: '密码最小长度不能小于5位'
                });
                return
            }
            if(this.ruleForm.pwdIsError===true){
                if(this.ruleForm.pwdErrorNum<1){
                    this.$message({
                        type: 'error',
                        message: '密码错误次数不能小于1'
                    });
                    return
                }
                if(this.ruleForm.pwdErrorTime<1){
                    this.$message({
                        type: 'error',
                        message: '限制登录时间不能小于1'
                    });
                    return  
                }
            }
            console.log(this.ruleForm)
            this.ruleForm.pwdComplexity = this.ruleForm.pwdComplexity.join(',')

            // this.ruleForm.pwdComplexity =""
            setSystemSafety(this.ruleForm).then(res => {
                if (res.code == 200) {
                    this.$message({
                        type: 'success',
                        message: '系统安全设置保存成功'
                    });
                } else {
                    this.$message({
                        type: 'error',
                        message: '系统安全设置保存失败'
                    });
                }
            });
            // 定时1秒
            setTimeout(() => {
                this.getSystemSafetyData()
            }, 500);
        },
        goback() {
            this.$router.go(-1);
        },
        // 获取系统安全设置
        getSystemSafetyData() {
            getSystemSafety().then(res => {
                if (res.data) {
                    this.ruleForm.id = res.data.id
                    let tempData = res.data;
                    tempData.pwdComplexity = tempData.pwdComplexity.split(',')
                    this.ruleForm = tempData;
                    // this.$set(this.ruleForm.pwdComplexity, tempData.pwdComplexity);
                    console.log(this.ruleForm.pwdComplexity, 2222222222)
                    this.$nextTick(() => {
                        // 数据更新后，确保表单回显
                    });
                }
            });
            this.$forceUpdate();
        }
    },
    mounted() {
        this.username = this.$store.state.user.name;
        this.ruleForm.pwdComplexity = []
        this.getSystemSafetyData()

    },
}
</script>
<style scoped>
.content {
    width: 100%;
    padding: 2% 2%;
}

.name {
    padding: 2% 2%;
}

.el-col-8 {
    width: 30%;
    margin-right: 2%;
}

.el-divider--horizontal {
    display: block;
    height: 2px;
    width: 100%;
    margin: 24px 0;
}

.el-divider__text {
    position: absolute;
    background-color: #FFFFFF;
    padding: 0 20px;
    font-weight: 500;
    color: #303133;
    font-size: 20px;
}

.el-form {
    margin-top: 1%
}
</style>
